/**
 * @param {number} k
 * @param {number} n
 * @return {number[][]}
 */

function sum(arr){
    if(arr.length==0) return 0
    let res =0
    for(let i=0;i<arr.length;i++){
        res+=arr[i]
    }
    return res
}
 var combinationSum3 = function(k, n) {
     let res = [],curr=[]
    const tools = (k,n,starIndex)=>{
        let s = sum(curr)
        if(s==n&&curr.length==k){
            res.push([...curr])
            return
        }
        for(let i=starIndex;i<=9;i++){
            curr.push(i)
            tools(k,n,i+1)
            curr.pop(i)
        }
    }
    tools(k,n,1)
    return res
};

console.log(combinationSum3(3,7));